<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table,
        tr,
        th,
        td {
            border: 1px solid #ccc;
        }

        tr {
            height: 40px;
        }

        table {
            border-collapse: collapse;
            width: 630px;
            text-align: center;
            margin: 20px auto;
        }

        thead {
            background-color: yellowgreen;
        }

        fieldset {
            width: 600px;
            margin: 0 auto;
        }

        input {
            height: 20px;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 第一部分：输入信息 -->
        <fieldset>
            <legend>学生录入系统</legend>
            <p>
                <span>姓名：</span>
                <input type="text" name="username" class="username">
            </p>
            <p>
                <span>年龄：</span>
                <input type="text" name="age" class="age">
            </p>
            <p>
                <span>性别：</span>
                <select name="sex" id="sex">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </p>
            <p>
                <span>手机：</span>
                <input type="text" name="phone" class="phone">
            </p>
            <p>
                <button id="but">确认提交</button>
            </p>

        </fieldset>
        <!-- 第二部分：信息展示 -->
        <table>
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>手机</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody class="tbody">
            </tbody>
            <script>
                document.getElementById("but").onclick = function() {
                    //获取输入值
                    var name = document.getElementsByName("username")[0].value;
                    var age = document.getElementsByName("age")[0].value;
                    var sex = document.getElementById("sex").value;
                    var phone = document.getElementsByName("phone")[0].value;
                    if(!name || !age || !phone) {
                        alert("请填写完整信息！");
                        return;
                    }
                    //获取表格body
                    var tbody = document.getElementsByClassName("tbody")[0];
                    
                    //创建新行并添加到表格
                    tbody.innerHTML += "<tr>" +
                        "<td>" + name + "</td>" +
                        "<td>" + age + "</td>" +
                        "<td>" + sex + "</td>" +
                        "<td>" + phone + "</td>" +
                        "<td><button onclick=\"del(this)\">删除</button></td>" +
                        "</tr>";
                };
                //删除行函数
                function del(obj) {
                    obj.parentNode.parentNode.remove();
                }
            </script>
    </div>
</body>


</html>